package system

import (
	"bottle/global"
	"bottle/model/common/response"
	"bottle/model/system"
	systemReq "bottle/model/system/request"
	"bottle/utils"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"time"
)

type BlacklistApi struct{}

func (b *BlacklistApi) AddBlacklist(c *gin.Context) {

	var req systemReq.BlacklistRequest
	err := c.ShouldBind(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(req, utils.BlacklistVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	var blacklist system.Blacklist
	err = global.GVA_DB.Where("user_id = ?", req.UserId).Where("operater_id = ?", req.OperaterID).Where("del = -1").First(&blacklist).Error
	if blacklist.ID != 0 {
		response.FailWithMessage("已拉黑该用户", c)
		return
	}

	blacklist = system.Blacklist{UserID: req.UserId, OperaterID: req.OperaterID, CreateTime: time.Now().Unix()}
	global.GVA_DB.Save(&blacklist)
	response.Ok(c)
	return
}

func (b *BlacklistApi) IsBlackUser(c *gin.Context) {
	var req systemReq.BlacklistRequest
	err := c.ShouldBind(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(req, utils.BlacklistVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	var blacklist system.Blacklist
	err = global.GVA_DB.Where("user_id = ?", req.UserId).Where("operater_id = ?", req.OperaterID).Where("del = -1").First(&blacklist).Error
	if err != nil {
		response.FailWithMessage("没有拉黑该用户", c)
		return
	}
	response.OkWithData(blacklist, c)
	return
}

func (b *BlacklistApi) DelBlacklist(c *gin.Context) {
	var req systemReq.BlacklistRequest
	err := c.ShouldBind(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(req, utils.BlacklistVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	var blacklist system.Blacklist
	err = global.GVA_DB.Where("user_id = ?", req.UserId).Where("operater_id = ?", req.OperaterID).Where("del = -1").First(&blacklist).Error
	if err != nil {
		response.FailWithMessage("没有拉黑该用户 不能取消拉黑", c)
		return
	}
	blacklist.Del = 1
	global.GVA_DB.Save(&blacklist)
	response.Ok(c)
	return
}

func (b *BlacklistApi) QueryBlacklist(c *gin.Context) {
	var req systemReq.IDReq
	err := c.ShouldBind(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(req, utils.BlacklistVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	var blacklist []system.Blacklist
	global.GVA_DB.Where("operater_id = ?", req.ID).Where("del = -1").Preload("User", func(db *gorm.DB) *gorm.DB {
		return db.Select("id,username,avatar,uuid")
	}).Find(&blacklist)
	response.OkWithData(blacklist, c)
	return
}

func (b *BlacklistApi) IsBlackOther(c *gin.Context)  {
	var req systemReq.IsBlackOtherRequest
	err := c.ShouldBind(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(req, utils.IsBlackOtherVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	var otherUser system.User
	global.GVA_DB.Where("uuid = ?", req.OtherUUID).First(&otherUser)
	var blackOther system.Blacklist
	var isBlacked system.Blacklist
	global.GVA_DB.Where("user_id = ?", req.UserId).Where("operater_id = ?", otherUser.ID).Where("del = ?", -1).First(&isBlacked)
	global.GVA_DB.Where("user_id = ?", otherUser.ID).Where("operater_id = ?", req.UserId).Where("del = ?", -1).First(&blackOther)
	if blackOther.ID != 0 {
		response.OkWithMessage("您已拉黑该用户", c)
		return
	}
	if isBlacked.ID != 0 {
		response.OkWithMessage("您被该用户拉黑", c)
		return
	}
	response.Fail(c)
	return
}